//
//  CommonVersion.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/4/13.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CommonVersion.h"

#import "CFolderMgr.h"
@implementation CommonVersion


-(id)init
{
    self = [super init];
    if(self)
    {
        
    }
    return self;
}



//每个数据库版本升级，这里增加一个函数。
-(void)update100:(FMDatabase* )db
{
    if(db != nil)
    {
     [db executeUpdate:@"CREATE TABLE IF NOT EXISTS SERVER (identifer integer PRIMARY KEY AUTOINCREMENT, IMIp varchar(256)  NOT NULL, IMPort INTEGER DEFAULT 0, OpenFireIP varchar(256) NOT NULL, OpenFirePort INTEGER DEFAULT 0,companyId varchar(256) NOT NULL,companyName varchar(256) NOT NULL,companyStatus varchar(256) NOT NULL, companyTelePhone varchar(256) NOT NULL,yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
      [db executeUpdate:@"CREATE TABLE IF NOT EXISTS ACCOUNT (identifer  INTEGER PRIMARY KEY AUTOINCREMENT, accountName varchar(50)  NOT NULL,userId varchar(50)  NOT NULL,info varchar(50)  NOT NULL,img varchar(50)  NOT NULL,msgNoticeSet varchar(50)  NOT NULL,dept varchar(50)  NOT NULL, passWord varchar(50)  NOT NULL, gesturePassword varchar(20) NOT NULL, loginStatus INTEGER DEFAULT 0, showInList INTEGER DEFAULT 0, companyId varchar(100)  NOT NULL, loginName varchar(100)  NOT NULL,yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
    }
    
    
}

-(void)update101:(FMDatabase* )db
{
    if(db != nil)
    {
        
    }
    
    
}

-(void)doDBUpdate:(NSInteger)version db:(FMDatabase*)db
{
    char actionName[128];
    sprintf(actionName, "update%d:", version);
    SEL action = sel_registerName(actionName);
    
    [self performSelector:action withObject:db];
}


- (void)updateCompanyDBVersions
{
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *adb) {
        
        [adb executeUpdate:@"CREATE TABLE IF NOT EXISTS CONTACT (identifer integer PRIMARY KEY AUTOINCREMENT, DEPTID varchar(32)  NOT NULL, UserName varchar(20)  NOT NULL, UserCode varchar(64)  NOT NULL, UserID varchar(32)  NOT NULL,Extension varchar(20)  NOT NULL,Autograph varchar(500)  NOT NULL,Position varchar(20)  NOT NULL,RoleID varchar(40)  NOT NULL,Telephone varchar(20)  NOT NULL,MobileTelephone varchar(20)  NOT NULL,Email varchar(50)  NOT NULL,HeadPortrait varchar(100)  NOT NULL,DEPNAME varchar(100)  NOT NULL,SEX varchar(100)  NOT NULL,IsDel INTEGER DEFAULT 0,birthDay varchar(100)  NOT NULL,address varchar(100)  NOT NULL,GroupID varchar(100)  NOT NULL, IsAdmin INTEGER DEFAULT 0, IsAvailable varchar(100)  NOT NULL, IsOftenContact INTEGER DEFAULT 0,info varchar(50)  NOT NULL,img varchar(50)  NOT NULL,msgNoticeSet varchar(50)  NOT NULL,Source varchar(50)  NOT NULL, PinYin varchar(200)  NOT NULL,messageSettingType INTEGER DEFAULT 0, compCode varchar(100)  NOT NULL,companyTelePhone varchar(100)  NOT NULL,pcAvailable varchar(100)  NOT NULL, firstLetter varchar(100)  NOT NULL,IsSelected INTEGER DEFAULT 0,yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [adb executeUpdate:@"CREATE TABLE IF NOT EXISTS DEPT (identifer integer PRIMARY KEY AUTOINCREMENT, DEPTID varchar(32)  NOT NULL, UserNum INTEGER DEFAULT 0, Sequence INTEGER DEFAULT 0, DepartName varchar(20)  NOT NULL, DepartFatherID varchar(32)  NOT NULL, IsDel INTEGER DEFAULT 0, InfoState varchar(40)  NOT NULL, IsSelected INTEGER DEFAULT 0,yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        [adb executeUpdate:@"CREATE TABLE IF NOT EXISTS PRESENCE (identifer integer PRIMARY KEY AUTOINCREMENT, UserCode varchar(32)  NOT NULL, pcAvailable varchar(100)  NOT NULL, IsAvailable varchar(100)  NOT NULL, yuLiuZiduan1 varchar(200)  NOT NULL,yuLiuZiduan2 INTEGER DEFAULT 0,yuLiuZiduan3 varchar(200)  NOT NULL);"];
        
        }];

    
}

-(void)updateDBVersions
{
    //    [db beginTransaction];
    
//    __block NSInteger dbversion = INIT_COMMONDBVERSION;  //默认初始版本
    
    //判断是否有系统配置表,如果没有配置表则建立配置表,并设置key = dbversion  value = INIT_DBVERSION
    
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *adb) {
        
        [self update100:adb];
        
//        FMResultSet *resultSet = [adb executeQuery:@"SELECT param_value FROM lc_system_cfg WHERE param_name=?", @"dbversion"];
//        if(resultSet == nil)
//        {
//            BOOL ret = [adb executeUpdate:@"CREATE TABLE lc_system_cfg (param_name varchar(32) NOT NULL, param_value varchar(255), PRIMARY KEY (param_name) ON CONFLICT REPLACE)"];
//            ret = [adb executeUpdate:@"INSERT INTO lc_system_cfg(param_name, param_value) VALUES (?, ?)", @"dbversion", [NSString stringWithFormat:@"%d", INIT_COMMONDBVERSION]];
//        }
//        else
//        {
//            while ([resultSet next]) {
//                NSString *param_name = [resultSet stringForColumn:@"param_name"];
//                NSString *param_value = [resultSet stringForColumn:@"param_value"];
//                if([param_name isEqualToString:@"dbversion"]);
//                {
//                    dbversion = [param_value integerValue];
//                    break;
//                }
//            }
//        }
    }];
    
//    NSLog(@"dbversion:%d", dbversion);
    
    
//    //判断当前的版本和配置表的版本号，如果当前的大于系统配置表中的数据则根据版本号进行数据更新。
//    for (NSInteger i = dbversion + 1 ; i <= NEW_COMMONDBVERSION; i++)
//    {
//        [queen inDatabase:^(FMDatabase *db)
//         {
//             [self doDBUpdate:i db:db];
//         }];
//        
//    }
//    //
//    //    //更新数据库的版本号
//    if (dbversion < NEW_COMMONDBVERSION)
//    {
//        [queen inDatabase:^(FMDatabase *db)
//         {
//             [db executeUpdate:@"UPDATE lc_system_cfg SET param_value = ? WHERE param_name = ?", [NSString stringWithFormat:@"%d", NEW_COMMONDBVERSION], @"dbversion"];
//         }];
//        
//        
//    }
    
    
}

@end
